package club.dataresources.controller;

import club.dataresources.bo.MenuBo;
import club.dataresources.bo.ProductBo;
import club.dataresources.bo.ServiceBo;
import club.dataresources.common.R;
import club.dataresources.entity.Menu;
import club.dataresources.entity.Product;
import club.dataresources.entity.Service;
import club.dataresources.service.MenuService;
import club.dataresources.service.ProductService;
import club.dataresources.service.ServiceService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @create 2018-11-04 21:33
 **/
@RequestMapping(value = "/admin/menu")
@Controller
public class AdminMenuController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ServiceService serviceService;

    @Autowired
    private MenuService menuService;

    @RequestMapping("/updateMenu")
    public @ResponseBody
    R updateMenu(@RequestBody Menu menu){
        System.out.println("================修改套餐================");
        try{
            if(menuService.updateMenu(menu)){
                return R.ok().put("code",200).put("msg","修改成功");
            }else {
                return R.error().put("code",500).put("msg","修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/removeMenu")
    public @ResponseBody
    R removeMenu(@RequestParam("menuId") Integer menuId){
        System.out.println("================新增套餐(menuId)================");
        try{
            if(menuService.removeMenu(menuId)){
                return R.ok().put("code",200).put("msg","删除成功");
            }else {
                return R.error().put("code",500).put("msg","删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/addMenu")
    public @ResponseBody
    R addMenu(@RequestBody Menu menu){
        System.out.println("================新增套餐================");
        try{
            if(menuService.addMenu(menu)){
                return R.ok().put("code",200).put("msg","显示完毕");
            }else {
                return R.error().put("code",500).put("msg","新增失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/listMenu")
    public @ResponseBody
    R listMenu(@RequestBody MenuBo menuBo){
        System.out.println("========后台========查询显示套餐================");
        try{
            Page<Menu> liPage = menuService.listMenu(menuBo.getPage(),menuBo.getRows(),menuBo);
            return R.ok().put("data",liPage.getResult()).put("code",200).put("msg","显示完毕").put("total",liPage.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/detailMenuById")
    public @ResponseBody
    R getMenuById(@RequestParam("menuId") Integer menuId){
        System.out.println("================根据menuId查询显示理发产品和理发类型 套餐内容================");
        try{
            Map<String,Object> map = new HashMap<String, Object>();
            map = menuService.detailMenuById(menuId);
            map.put("menuId",menuId);
            return R.ok().put("data",map).put("code",200).put("msg","显示完成");
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/listProduct")
    public @ResponseBody
    R listProduct(HttpServletRequest request,
                  HttpServletResponse response,
                  @RequestBody ProductBo productBo){
        System.out.println("================查询理发产品的结果================");
        try {
            Page<Product> liPage = productService.listProduct(productBo.getPage(),productBo.getRows(),productBo);
            return R.ok().put("data",liPage.getResult()).put("code",200).put("msg","显示完毕").put("total",liPage.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/listService")
    public @ResponseBody
    R listService(HttpServletRequest request,
                  HttpServletResponse response,
                  @RequestBody ServiceBo serviceBo){
        System.out.println("================查询理发类型的结果================");
        try {
            Page<Service> list = serviceService.listService(serviceBo.getPage(),serviceBo.getRows(),serviceBo);
            return R.ok().put("data",list.getResult()).put("code",200).put("msg","显示完毕").put("total",list.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/addProduct")
    public @ResponseBody
    R addProduct(HttpServletRequest request,
                 HttpServletResponse response,
                 @RequestBody Product product){
        System.out.println("=============添加理发产品============");
        response.setHeader("Access-Control-Allow-Origin", "*");
        try{
            if(productService.addProduct(product)){
                return R.ok().put("code",200).put("msg","添加成功");
            }else{
                return R.ok().put("code",500).put("msg","添加失败,addProduct返回false");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/updateProduct")
    public @ResponseBody
    R updateProduct(@RequestBody Product product){
        System.out.println("===================修改理发产品==================");
        try {
            if(productService.updateProduct(product)){
                return R.ok().put("code",200).put("msg","修改成功");
            }else{
                return R.ok().put("code",500).put("msg","修改失败,updateProduct返回false");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/removeProduct")
    public @ResponseBody
    R removeProduct(@RequestParam("productId") Integer productId){
        System.out.println("======================删除理发产品=======================");
        try{
            if(productService.removeProduct(productId)){
                return R.ok().put("code",200).put("msg","删除成功");
            }else{
                return R.ok().put("code",500).put("msg","删除失败,removeProduct返回true");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/addService")
    public @ResponseBody
    R addService(@RequestBody Service service){
        System.out.println("===================添加理发服务==================");
        try {
            if(serviceService.updateService(service)){
                return R.ok().put("code",200).put("msg","添加成功");
            }else{
                return R.ok().put("code",500).put("msg","添加失败,addService返回false");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/updateService")
    public @ResponseBody
    R updateService(@RequestBody Service service){
        System.out.println("===================修改理发产品==================");
        try {
            if(serviceService.updateService(service)){
                return R.ok().put("code",200).put("msg","修改成功");
            }else{
                return R.ok().put("code",500).put("msg","修改失败,serviceServiceu返回false");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/removeService")
    public @ResponseBody
    R removeService(@RequestParam("serviceId") Integer serviceId){
        System.out.println("======================删除理发产品=======================");
        try{
            if(serviceService.removeService(serviceId)){
                return R.ok().put("code",200).put("msg","删除成功");
            }else{
                return R.ok().put("code",500).put("msg","删除失败,removeProduct返回true");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }
}
